package com.youdoneed.community.forumConfig.service;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.youdoneed.base.application.AppException;
import com.youdoneed.community.common.utils.StringUUIdUtil;
import com.youdoneed.community.forum.entity.Forumallowviewrecord;
import com.youdoneed.community.forum.service.ForumallowviewrecordService;
import com.youdoneed.community.forumConfig.dao.ForumuserprivacyconfigMapper;
import com.youdoneed.community.forumConfig.entity.Forumuserprivacyconfig;
import com.youdoneed.community.group.entity.Forumgroup;

/**
 * 用户帖子隐私设置服务接口
 * 公开状态：0：公开；1：对好友公开；2：部分好友不可见；3：部分好友可见;4:仅自己可见
 * @author lid
 * @date 2017.6.28
 */
@Service
public class ForumuserprivacyconfigService {
	
	private final Logger logger = LoggerFactory.getLogger(ForumuserprivacyconfigService.class);
	
	//不允许查看
	private static final Integer notAllowed = 0;
	
	//允许查看
	private static final Integer allowed = 1;
	
	//公开
	private static final Integer publicity = 0;
	
	//对好友公开
	private static final Integer openToAllFriends = 1;
	
	//部分好友不可见
	private static final Integer openToPartFriends = 2;
	
	//部分好友可见
	private static final Integer closeToPartFriends = 3;
	
	//私密
	private static final Integer privacy = 4;
	
	//指定群组
	private static final Integer groups = 5;
	
	@Autowired
	ForumuserprivacyconfigMapper forumuserprivacyconfigMapper;
	
	@Autowired
	ForumallowviewrecordService forumallowviewrecordService;
	
	/**
	 * 保存隐私设置
	 * @param privacyConfig
	 * @return int
	 * @author lid
	 * @date 2017.6.28
	 */
	@Transactional
	public int saveUserPrivacyConfig(Forumuserprivacyconfig privacyConfig){
		logger.info("saveUserPrivacyConfig");
		if(null == privacyConfig){
			logger.info("privacyConfig参数为空！");
			throw new AppException("privacyConfig参数为空！");
		}
		
		if(!StringUtils.hasText(privacyConfig.getUserId())){
			logger.info("userId参数为空！");
			throw new AppException("userId参数为空！");
		}
		
		if(null == privacyConfig.getAllowViewStatus()){
			logger.info("allowViewStatus参数为空！");
			throw new AppException("allowViewStatus参数为空！");
		}
		
		/**
		 * 如果设置为2：部分好友不可见；3：部分好友可见，那么必须同时携带listForumallowviewrecord参数
		 */
		if(((openToPartFriends == privacyConfig.getAllowViewStatus())
				|| (closeToPartFriends == privacyConfig.getAllowViewStatus()))
				&& ((null == privacyConfig.getListForumallowviewrecord())
						|| (0 == privacyConfig.getListForumallowviewrecord().size()))){
			logger.info("listForumallowviewrecord参数为空！");
			throw new AppException("listForumallowviewrecord参数为空！");
		}
		
		/**
		 * 如果设置为4：群组可见，那么必须携带listForumgroup参数
		 */
		if((groups == privacyConfig.getAllowViewStatus())
				&& ((null == privacyConfig.getListForumgroup())
						|| (0 == privacyConfig.getListForumgroup().size()))){
			logger.info("listForumgroup参数为空！");
			throw new AppException("listForumgroup参数为空！");
		}
		
		try {
			//判断当前用户是否已经有隐私设置
			Forumuserprivacyconfig config = forumuserprivacyconfigMapper.selectByUserId(privacyConfig.getUserId());
			if(null != config){
				logger.info("当前用户以存在帖子隐私设置！");
				forumuserprivacyconfigMapper.deleteByPrimaryKey(config.getForumUserPrivacyConfigId());
			}
			
			String forumUserPrivacyConfigId = StringUUIdUtil.getUUID();
			privacyConfig.setForumUserPrivacyConfigId(forumUserPrivacyConfigId);
			privacyConfig.setCreateDate(new Date());
			
			int result = forumuserprivacyconfigMapper.insert(privacyConfig);
			logger.info("保存隐私设置成功！");

			if((openToPartFriends == privacyConfig.getAllowViewStatus())
					|| (closeToPartFriends == privacyConfig.getAllowViewStatus())){
				for(Forumallowviewrecord record : privacyConfig.getListForumallowviewrecord()){
					forumallowviewrecordService.saveForumallowviewrecord(record);
				}
				logger.info("设置好友可见成功！");
			}
			
			if(groups == privacyConfig.getAllowViewStatus()){
				for(Forumgroup group : privacyConfig.getListForumgroup()){
					Forumallowviewrecord record = new Forumallowviewrecord();
					record.setAllowedStatus(allowed);
					record.setForumGroupId(group.getForumGroupId());
					forumallowviewrecordService.saveForumallowviewrecord(record);
				}
				logger.info("设置群组可见成功！");
			}
			return result;
			
		} catch (Exception e) {
			logger.info("保存隐私设置异常！"+e.getMessage());
			return 0;
		}
	}
	
	/**
	 * 根据用户id获取隐私设置
	 * @param userId
	 * @return Forumuserprivacyconfig
	 * @author lid
	 * @date 2017.6.28
	 */
	//TODO  完善隐私设置
	
	@Transactional
	public Forumuserprivacyconfig getPrivacyConfigByUserId(String userId){
		if(!StringUtils.hasText(userId)){
			logger.info("userId参数为空！");
			throw new AppException("userId参数为空！");
		}
		try {
			Forumuserprivacyconfig config = forumuserprivacyconfigMapper.selectByUserId(userId);
			return config;
		} catch (Exception e) {
			logger.info("根据用户id获取隐私设置异常！"+e.getMessage());
			return null;
		}
	}
}
